﻿/*
 * Thống kê
 * undone
 * btnPrintReport_Click
 * btnINreport_Click

 */

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using MigraDoc.DocumentObjectModel;
using DTO;
using BLL;

namespace GUI.View
{
    public partial class ThongKe : UserControl
    {
        private ExportToPdf pdf;
        private ThongKeBLL thongkeBLL;

        private AppCommands _Commands;
        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public AppCommands Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    AppCommands oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);

                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(AppCommands oldValue, AppCommands newValue)
        {
            if (newValue != null)
            {
                btnCancel.Command = newValue.ThongKeCommands.Cancel;
            }
            else
            {
                btnCancel.Command = null;
            }
        }


        public ThongKe()
        {
            InitializeComponent();
            thongkeBLL = new ThongKeBLL();
        }

        private void btnBaoCao_Click(object sender, EventArgs e)
        {
            int _thangbaocao = Int32.Parse(cbbThang.Text);
            int _nambaocao = Int32.Parse(cbbNam.Text);
            loadTobuom(_thangbaocao, _nambaocao);
            loadbang(_thangbaocao, _nambaocao);
            loadbao(_thangbaocao, _nambaocao);
        }

        private void ThongKe_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 12; i++)
            {
                cbbxMonth.Items.Add(i + 1);
                cbbThang.Items.Add(i + 1);
            }

            for (int i = 0; i < 150; i++)
            {
                cbbxYear.Items.Add(2000 + i);
                cbbNam.Items.Add(2000 + i);
            }
            cbbxMonth.Text = DateTime.Now.Month.ToString();
            cbbxYear.Text = DateTime.Now.Year.ToString();
            cbbThang.Text = DateTime.Now.Month.ToString();
            cbbNam.Text = DateTime.Now.Year.ToString();
            saveFileDialog.DefaultExt = "pdf";
            saveFileDialog.Title = "Save file pdf";
            saveFileDialog.Filter = "pdf file (.pdf)|*.pdf|txt file (.txt )|*.txt|CSharp (.cs)|*.cs|C++(.cpp)|*.cpp| All file(*.*)|*.*";
        }

        #region Load Data
        public void loadTobuom(int _thangbaocao, int _nambaocao)
        {
            DataSet ds = thongkeBLL.ToBuom_GetDSKhachHang(_thangbaocao, _nambaocao);
            if (ds == null)
                return;

            DataTable dt = ds.Tables[0];
            
            DataSet DSKhachHangDaDK =thongkeBLL.ToBuom_GetDSKhachHangDaGiaHan(_thangbaocao,_nambaocao);
            if (DSKhachHangDaDK == null)
            {
                dgvDichVuQuangCaoToBuom.DataSource = dt;

                for (int i = 0; i < dgvDichVuQuangCaoToBuom.Rows.Count - 1; i++)
                {
                    dgvDichVuQuangCaoToBuom.Rows[i].Cells[0].Value = i + 1;
                }
                lblKhachHang2.Text = (dgvDichVuQuangCaoToBuom.Rows.Count - 1) + " Khách Hàng";
            }
            else
            {
                DataTable dt1 = DSKhachHangDaDK.Tables[0];
                dt.Merge(dt1);
                dgvDichVuQuangCaoToBuom.DataSource = dt;
                for (int i = 0; i < dgvDichVuQuangCaoToBuom.Rows.Count - 1; i++)
                {

                    dgvDichVuQuangCaoToBuom.Rows[i].Cells[0].Value = i + 1;

                }
                lblKhachHang2.Text = (dgvDichVuQuangCaoToBuom.Rows.Count - 1) + " Khách Hàng";
            }
        }
        public void loadbao(int _thangbaocao, int _nambaocao)
        {
            DataSet ds1 = thongkeBLL.Bao_GetDSKhachHang(_thangbaocao,_nambaocao);
            if (ds1 == null)
            {
                return;
            }
            DataTable dt1 = ds1.Tables[0];

            DataSet ds2 = thongkeBLL.Bao_GetDSKhachHangDaGiaHan(_thangbaocao, _nambaocao);
            if (ds2 == null)
            {
                dgvDichVuQuangCaobao.DataSource = dt1;

                for (int i = 0; i < dgvDichVuQuangCaobao.Rows.Count - 1; i++)
                {

                    dgvDichVuQuangCaobao.Rows[i].Cells[0].Value = i + 1;

                }
                lblKhachhang.Text = (dgvDichVuQuangCaobao.Rows.Count - 1) + " Khách Hàng";
            }
            else
            {
                DataTable dt2 = ds2.Tables[0];
                dt1.Merge(dt2);
                dgvDichVuQuangCaobao.DataSource = dt1;
                for (int i = 0; i < dgvDichVuQuangCaobao.Rows.Count - 1; i++)
                {

                    dgvDichVuQuangCaobao.Rows[i].Cells[0].Value = i + 1;

                }
                lblKhachhang.Text = (dgvDichVuQuangCaobao.Rows.Count - 1) + " Khách Hàng";
            }
        }
        public void loadbang(int _thangbaocao, int _nambaocao)
        {
            DataSet ds = thongkeBLL.Bang_GetDSKhachHang(_thangbaocao, _nambaocao);
            if (ds == null)
            {
                return;
            }
            DataTable dt = ds.Tables[0];
            
            DataSet ds1 = thongkeBLL.Bang_GetDSKhachHangDaGiaHan(_thangbaocao, _nambaocao);
            if (ds1 == null)
            {
                dgvDichVuQuangCaobang.DataSource = dt;

                for (int i = 0; i < dgvDichVuQuangCaobang.Rows.Count - 1; i++)
                {
                    dgvDichVuQuangCaobang.Rows[i].Cells[0].Value = i + 1;
                }
                lblKhachHang1.Text = (dgvDichVuQuangCaobang.Rows.Count - 1) + " Khách Hàng";
            }
            else
            {
                DataTable dt1 = ds1.Tables[0];
                dt.Merge(dt1);
                dgvDichVuQuangCaobang.DataSource = dt;
                for (int i = 0; i < dgvDichVuQuangCaobang.Rows.Count - 1; i++)
                {
                    dgvDichVuQuangCaobang.Rows[i].Cells[0].Value = i + 1;
                }
                lblKhachHang1.Text = (dgvDichVuQuangCaobang.Rows.Count - 1) + " Khách Hàng";
            }
        }
        #endregion

        #region Event
        
        private void btnPrintReport_Click(object sender, EventArgs e)
        {
            pdf = new ExportToPdf(dgvDichVuQuangCaobao, dgvDichVuQuangCaobang, dgvDichVuQuangCaoToBuom);
            //pdf = new ExportToPdf(dgvDichVuQuangCaobao);
            Document document = pdf.CreateDocumentThongKeKhachhangTheoDichVu(lblKhachhang.Text, lblKhachHang1.Text, lblKhachHang2.Text);
            //pdf = new ExportToPdf(dgvThongKeDoanhThu);
            //Document document1 = pdf.CreateDocumentBaoCaoDoanhThu(cbbxMonth.Text, cbbxYear.Text, lblTongSoDoangThu.Text);


            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filepath = saveFileDialog.FileName;
                pdf.SaveFilePdf(document, filepath);
                //pdf.SaveFilePdf(document1, filepath);
            }
            else
            {
                MessageBox.Show("love");
            }
        }
             
        private void btnINreport_Click(object sender, EventArgs e)
        {
            pdf = new ExportToPdf(dgvThongKeDoanhThu);
            Document document = pdf.CreateDocumentBaoCaoDoanhThu(cbbxMonth.Text, cbbxYear.Text, lblTongSoDoangThu.Text);
            //pdf = new ExportToPdf(dgvThongKeDoanhThu);
            //Document document1 = pdf.CreateDocumentBaoCaoDoanhThu(cbbxMonth.Text, cbbxYear.Text, lblTongSoDoangThu.Text);


            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filepath = saveFileDialog.FileName;
                pdf.SaveFilePdf(document, filepath);
                //pdf.SaveFilePdf(document1, filepath);
            }
            else
            {
                MessageBox.Show("love");
            }
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            int _thangbaocao = Int32.Parse(cbbxMonth.Text);
            int _nambaocao = Int32.Parse(cbbxYear.Text);
            //string querydoanhthu = String.Format("select HoTen,SoTien from KHACHHANG,DIAOC,PHIEUDANGKY where KHACHHANG.MaKH=DIAOC.MAKH and DIAOC.MaDiaOc=PHIEUDANGKY.MaDiaOc and MONTH(TuNgay)='{0}' and YEAR(TuNgay)='{1}'", _thangbaocao, _nambaocao);
            DataSet ds = thongkeBLL.DoanhThu_GetDSKhachHang(_thangbaocao, _nambaocao);
            if (ds == null)
            {
                return;
            }

            DataTable dt = ds.Tables[0];

            //string querydoanhthu1 = String.Format("select HoTen,TongTienGiaHan as SoTien from KHACHHANG,DIAOC,PHIEUDANGKY,PHIEUGIAHAN where KHACHHANG.MaKH=DIAOC.MAKH and DIAOC.MaDiaOc=PHIEUDANGKY.MaDiaOc and PHIEUDANGKY.MaPhieuDangKy=PHIEUGIAHAN.MaPhieuDangKy and MONTH(TuNgay)='{0}' and YEAR(TuNgay)='{1}'", _thangbaocao, _nambaocao);
            DataSet ds1 = thongkeBLL.DoanhThu_GetDSKhachHangDaGiaHan(_thangbaocao, _nambaocao);
            if (ds1 == null)
            {
                dgvThongKeDoanhThu.DataSource = dt;
                //dgvThongKeDoanhThu.DataSource = CompareDataTables(dt, dt1);
                double _tongtien = 0;
                for (int i = 0; i < dgvThongKeDoanhThu.Rows.Count - 1; i++)
                {
                    dgvThongKeDoanhThu.Rows[i].Cells[0].Value = i + 1;
                    _tongtien += double.Parse(dgvThongKeDoanhThu.Rows[i].Cells["SoTien"].Value.ToString());
                }
                lblTongSoDoangThu.Text = _tongtien + " VND";
            }
            else
            {
                DataTable dt1 =ds1.Tables[0];
                dt.Merge(dt1);
                dgvThongKeDoanhThu.DataSource = dt;
                //dgvThongKeDoanhThu.DataSource = CompareDataTables(dt, dt1);
                double _tongtien = 0;
                for (int i = 0; i < dgvThongKeDoanhThu.Rows.Count - 1; i++)
                {
                    dgvThongKeDoanhThu.Rows[i].Cells[0].Value = i + 1;
                    _tongtien += double.Parse(dgvThongKeDoanhThu.Rows[i].Cells["SoTien"].Value.ToString());
                }
                lblTongSoDoangThu.Text = _tongtien + " VND";
            }     
        }

        #endregion

        public DataTable CompareDataTables(DataTable first, DataTable second)
        {
            first.TableName = "FirstTable";
            second.TableName = "SecondTable";

            DataTable table = new DataTable("Difference");

            try
            {
                using (DataSet ds = new DataSet())
                {
                    ds.Tables.AddRange(new DataTable[] { first.Copy(), second.Copy() });

                    DataColumn[] firstcolumns = new DataColumn[ds.Tables[0].Columns.Count];

                    for (int i = 0; i < firstcolumns.Length; i++)
                    {
                        firstcolumns[i] = ds.Tables[0].Columns[i];
                    }

                    DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count];

                    for (int i = 0; i < secondcolumns.Length; i++)
                    {
                        secondcolumns[i] = ds.Tables[1].Columns[i];
                    }

                    DataRelation r = new DataRelation(string.Empty, firstcolumns, secondcolumns, false);

                    ds.Relations.Add(r);

                    for (int i = 0; i < first.Columns.Count; i++)
                    {
                        table.Columns.Add(first.Columns[i].ColumnName, first.Columns[i].DataType);
                    }

                    table.BeginLoadData();

                    foreach (DataRow parentrow in ds.Tables[0].Rows)
                    {
                        DataRow[] childrows = parentrow.GetChildRows(r);
                        if (childrows == null || childrows.Length == 0)
                            table.LoadDataRow(parentrow.ItemArray, true);
                    }
                    table.EndLoadData();
                }
            }

            catch (Exception ex)
            {             
                MessageBox.Show("error: \n"+ex);
            }

            return table;
        }
    
    }
}
